home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et3_0-a1.lha / et3 / src / WindowSystem.C < prev    next >
C/C++ Source or Header  |  1992-08-26  |  7KB  |  318 lines

  1. #ifdef __GNUG__
  2. #pragma implementation
  3. #endif
  4.  
  5. #include "WindowSystem.h"
  6.  
  7. #include "Class.h"
  8. #include "CLib.h"
  9. #include "String.h"
  10. #include "Error.h"
  11. #include "WindowPort.h"
  12. #include "ClipBoard.h"
  13. #include "DevBitmap.h"
  14. #include "StreamConnection.h"
  15. #include "OrdColl.h"
  16. #include "ClassManager.h"
  17. #include "Env.h"
  18. #include "Look.h"
  19. #include "TextItem.h"
  20. #include "CompVObject.h"
  21. #include "Data.h"
  22. #include "MemBuf.h"
  23.  
  24. extern StreamConnection *gRpc;
  25.  
  26. WindowSystem    *gWindowSystem;
  27. int             gDepth= 1;
  28. bool            gColor;
  29. int             gMaxDepth;
  30. bool            gGreyScale;
  31.  
  32. //---- RemoteData --------------------------------------------------------------
  33.  
  34. class RemoteData : public StreamBufData {
  35.     int holder;
  36.     Class *w;
  37. public:
  38.     RemoteData(int from, Symbol t, Class *cl);
  39.     StreamBuf *GetStreamBuf();
  40.     bool CanConvert(Class *want);
  41.     Object *AsObject(Class *want);
  42.     bool IsETFormat()
  43.     { return TRUE; }
  44. };
  45.  
  46. RemoteData::RemoteData(int from, Symbol t, Class *cl) : StreamBufData(0, t)
  47. {
  48.     holder= from;
  49.     classtype= cl;
  50. }
  51.  
  52. StreamBuf *RemoteData::GetStreamBuf()
  53. {
  54.     if (sb == 0) {
  55.     char *buf= 0;
  56.     int len= 0;
  57.     // get clipboard from server
  58.     gRpc->Talk(holder, "clipboard:get",
  59.         form("%s %s", cDocTypeET.AsString(), w->Name()), -1, &buf, &len);
  60.     if (len > 0) {
  61.         sb= new MemBuf;
  62.         sb->sputn(buf, len);
  63.         ((MemBuf*)sb)->SwitchToRead();
  64.     }
  65.     SafeDelete(buf);
  66.     }
  67.     return sb;
  68. }
  69.  
  70. bool RemoteData::CanConvert(Class *want)
  71. {
  72.     bool can= FALSE;
  73.     char *ret= 0;
  74.     int retlen= 0;
  75.     gRpc->Talk(holder, "clipboard:canconvert", want->Name(), -1, &ret, &retlen);
  76.     if (ret)
  77.     can= strcmp(ret, "yes") == 0;
  78.     SafeDelete(ret);
  79.     return can;
  80. }
  81.  
  82. Object *RemoteData::AsObject(Class *want)
  83. {
  84.     if (gDebug)
  85.     fprintf(stderr, "RemoteData::AsObject: is:%s want:%s %d\n",
  86.             Type().AsString(), want->Name(), IsETFormat());
  87.     w= want;
  88.     return Data::AsObject(want);
  89. }
  90.  
  91. //---- Clipboard support -------------------------------------------------------
  92.  
  93. int WindowSystem::DevHaveSelection(Data *data)
  94. {
  95.     char *args= data->Type().AsString();
  96.     if (data->GetClassType())
  97.     args= form("%s %s", args, data->GetClassType()->Name());
  98.     gRpc->Talk(0, "clipboard:holder", args);
  99.     return 0;
  100. }
  101.  
  102. void WindowSystem::ExtCommand(int from, char *req, char *args, int,
  103.                             char *&ret, int &retlen)
  104. {
  105.     Class *theclasstype= 0;
  106.     char type[100], classtype[100];
  107.  
  108.     if (strcmp(req, "get") == 0) {
  109.     if (sscanf(args, "%s %s", type, classtype) == 2)
  110.         theclasstype= gClassManager->Find(classtype);
  111.     ret= gClipBoard->GetExtSelection(&retlen, Symbol(type), theclasstype);
  112.  
  113.     } else if (strcmp(req, "holder") == 0) {
  114.     if (sscanf(args, "%s %s", type, classtype) == 2)
  115.         theclasstype= gClassManager->Find(classtype);
  116.     gClipBoard->SetData(new RemoteData(from, Symbol(type), theclasstype));
  117.  
  118.     } else if (strcmp(req, "remove") == 0) {
  119.     gClipBoard->SetData(0);
  120.  
  121.     } else if (strcmp(req, "canconvert") == 0) {
  122.     bool can= FALSE;
  123.     if (sscanf(args, "%s", classtype) == 1) {
  124.         theclasstype= gClassManager->Find(classtype);
  125.         if (theclasstype) {
  126.         Data *data= gClipBoard->GetType();
  127.         if (data)
  128.             can= data->CanConvert(theclasstype);
  129.         }
  130.     }
  131.     ret= can ? "yes" : "no";
  132.     }
  133. }
  134.  
  135. void WindowSystem::DevCheckSelection()
  136. {
  137. }
  138.  
  139. //---- MenuBorderItem ----------------------------------------------------------
  140.  
  141. class MenuBorderItem: public CompositeVObject {
  142.     Layout *l;
  143. public:
  144.     MenuBorderItem(VObject *inner, VObject *title= 0)
  145.                 : CompositeVObject(cIdNone, inner, title, 0)
  146.     { l= title ? gLook->PopUpMenuLayout() : gLook->PullDownMenuLayout(); }
  147.     ~MenuBorderItem()
  148.     { Remove(At(0));  /* avoid deletion of inner */ }
  149.     void Draw(Rectangle r)
  150.     { l->Adorn(this, r);  }
  151.     Metric GetMinSize()
  152.     { return l->GetMinSize(this); }
  153.     void SetOrigin(Point at)
  154.     { VObject::SetOrigin(at); l->SetOrigin(this, at); }
  155.     void SetExtent(Point e)
  156.     { VObject::SetExtent(e); l->SetExtent(this, e); }
  157. };
  158.  
  159. //---- WSInit ------------------------------------------------------------------
  160.  
  161. bool testwindow= FALSE;
  162.  
  163. void WindowSystem::WSInit()
  164. {
  165.     if (gWindowSystem)
  166.     return;
  167.  
  168.     InitSystem();
  169.     
  170.     if (! gInMain) {
  171.     fprintf(stderr, "--- InitWindowSystem called before ETInit\n");
  172.     CLib::Abort();
  173.     }
  174.     
  175.     batch= Env::GetValue("WindowSystem.DoubleBuffer", TRUE);
  176.  
  177.     gMaxDepth= Env::GetValue("WindowSystem.MaxDepth", 32);
  178.     gGreyScale= Env::GetValue("WindowSystem.GreyScale", FALSE);
  179.  
  180.     testwindow= Env::GetValue("WindowSystem.Test", FALSE);
  181.  
  182.     if (gWindowSystem == 0 && CLib::Getenv("DISPLAY"))
  183.     gWindowSystem= (WindowSystem*) gClassManager->Load("xserver", "XWinSystem");
  184.  
  185.     if (gWindowSystem == 0 && CLib::Getenv("WINDOW_PARENT"))
  186.     gWindowSystem= (WindowSystem*) gClassManager->Load("sun", "SunWindowSystem");
  187.  
  188.     if (gWindowSystem == 0) {
  189.     fprintf(stderr, "can't find window system\n");
  190.     CLib::Exit(0);    // give up
  191.     }
  192.     
  193.     gWindowSystem->Init();
  194. }
  195.  
  196. //---- WindowSystem ------------------------------------------------------------
  197.  
  198. NewMetaImpl(WindowSystem, SysEvtHandler, (TP(allwins),
  199.     T(lasttime), TB(fullscreen), TP(grabport), T(Clicks), T(screenRect), T(batch)));
  200.  
  201. bool WindowSystem::batch;
  202. u_long WindowSystem::lasttime;
  203. bool WindowSystem::fullscreen;
  204. WindowPort *WindowSystem::grabport;
  205. Rectangle WindowSystem::screenRect= Rectangle(0, 0, 1152, 900);
  206. Token WindowSystem::lastClick;
  207. int WindowSystem::Clicks;
  208. OrdCollection *WindowSystem::allwins;
  209. WindowColorMap *WindowSystem::wcmap;
  210.  
  211. WindowSystem::WindowSystem() : SysEvtHandler(0)
  212. {
  213. }
  214.  
  215. WindowSystem::~WindowSystem()
  216. {
  217.     if (gWindowSystem == this)
  218.     gWindowSystem= 0;
  219.     SafeDelete(gFontManager);
  220.     SafeDelete(gInkManager);
  221.     SafeDelete(allwins);
  222.     //SafeDelete(wcmap);
  223. }
  224.  
  225. void WindowSystem::InitNew()
  226. {
  227.     if (allwins == 0)
  228.     allwins= new OrdCollection;
  229. }
  230.  
  231. void WindowSystem::Init()
  232. {
  233.     gColor= gDepth > 1;
  234.  
  235.     gFontManager= MakeFontManager();
  236.     if (gFontManager == 0)
  237.     Fatal("Init", "no font manager");
  238.     
  239.     if (gFontManager->Init())
  240.     Fatal("Init", "initialization of font manager failed");
  241.     
  242.     gInkManager= new InkManager();
  243.     if (gDepth > 1) {
  244.     gHighlightColor= new_RGBColor(0, 255, 0); // light green
  245.     Env::Bind(gHighlightColor, "WindowSystem.HighlightColor");
  246.     } else
  247.     gHighlightColor= gInkXor;
  248.     MakeLook(Env::GetValue("WindowSystem.Motif", 0));
  249. }
  250.  
  251. VObject *WindowSystem::MakeWindowBorder(WindowType wt, VObject *in,
  252.                             char *title, bool, bool)
  253. {
  254.     switch (wt) {
  255.     case eWinPulldownMenu:
  256.     return new MenuBorderItem(in);
  257.     case eWinPopupMenu:
  258.     return new MenuBorderItem(in, new TextItem(title));
  259.     default:
  260.     return in;
  261.     }
  262. }
  263.  
  264. void WindowSystem::SetWait()
  265. {
  266.     allwins->ForEach(WindowPort,DevSetCursor)(eCrsHourglass);
  267. }
  268.  
  269. void WindowSystem::ResetWait()
  270. {
  271.     Iter next(allwins);
  272.     WindowPort *wp;
  273.     
  274.     while (wp= (WindowPort*) next())
  275.     wp->DevSetCursor(wp->GetCursor());
  276. }
  277.  
  278. void WindowSystem::Update()
  279. {
  280.     allwins->ForEach(WindowPort,DevUpdate)();
  281. }
  282.  
  283. WindowPort *WindowSystem::MakeWindow(Window*, WindowType, GrCursor) 
  284. {
  285.     return 0;
  286. }
  287.  
  288. void WindowSystem::RemoveWindow(WindowPort *wp)
  289. {
  290.     if (allwins)
  291.     allwins->RemovePtr(wp);
  292. }
  293.  
  294. void WindowSystem::AddWindow(WindowPort *wp)
  295. {
  296.     if (allwins)
  297.     allwins->Add(wp);
  298. }
  299.  
  300. FontManager *WindowSystem::MakeFontManager()
  301. {
  302.     return 0;
  303. }
  304.  
  305. DevBitmap *WindowSystem::MakeDevBitmap(Point, u_short*, u_short, bool, u_short)
  306. {
  307.     return 0;
  308. }
  309.  
  310. void WindowSystem::graphicDelay(u_int duration)
  311. {
  312.     gSystem->Wait(duration);
  313. }
  314.  
  315. void WindowSystem::DevBell(long)
  316. {
  317. }
  318.